<template>
    <div class="app-container">
        <el-form v-show="showSearch" ref="queryParamsRef" :inline="true" :model="queryParams" label-width="100px">
            <el-form-item label="字典名称" prop="baseDictName">
                <el-input v-model="queryParams.baseDictName" clearable placeholder="请输入字典名称"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="字典类型" prop="dictType">
                <el-input v-model="queryParams.dictType" clearable placeholder="请输入字典类型"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-radio-group v-model="queryParams.status" @change="handleQuery">
                    <el-radio value="">全部</el-radio>
                    <el-radio v-for="dict in dictDataMap['sys_normal_disable']" :key="dict.dictValue"
                        :value="dict.dictValue">{{
                            dict.dictLabel }}
                    </el-radio>
                </el-radio-group>
            </el-form-item>
            <div style="float: right" class="mr20">
                <query-form-button @handleQuery="handleQuery" @resetQuery="resetQuery" />
            </div>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button v-hasPerm="['system:sysBaseDictType:add']" plain type="primary" @click="handleAdd"><template
                        #icon>
                        <i-ep-plus />
                    </template>新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button v-hasPerm="['system:sysBaseDictType:edit']" :disabled="single" plain type="success"
                    @click="handleUpdate"><template #icon>
                        <i-ep-edit />
                    </template>修改
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button v-hasPerm="['system:sysBaseDictType:remove']" :disabled="multiple" plain type="danger"
                    @click="handleDelete"><template #icon>
                        <i-ep-delete />
                    </template>删除
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button v-hasPerm="['system:sysBaseDictType:export']" plain type="warning"
                    @click="handleExport"><template #icon>
                        <i-ep-download />
                    </template>导出
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button plain type="danger" @click="refreshCacheDictTypeFun">
                    <template #icon>
                        <i-ep-refresh />
                    </template>
                    刷新缓存
                </el-button>
            </el-col>
        </el-row>

        <el-table v-adaptive :data="sysBaseDictTypeList" :border="true" :stripe="true" highlight-current-row
            @selection-change="handleSelectionChange" :header-cell-style="$headerCellStyle">
            <el-table-column :align="'center'" type="selection" width="55" />
            <el-table-column :align="'center'" label="字典名称" prop="baseDictName" />
            <el-table-column :header-align="'center'" label="字典类型" show-overflow-tooltip>
                <template #default="scope">
                    <router-link :to="'/system/base-dict-data/index/' + scope.row.baseDictId" class="link-type">
                        <span>{{ scope.row.dictType }}</span>
                    </router-link>
                </template>
            </el-table-column>
            <el-table-column :align="'center'" label="状态" prop="status">
                <template #default="scope">
                    <dict-data-tag :options="dictDataMap['sys_normal_disable']" :value="scope.row.status" />
                </template>
            </el-table-column>
            <el-table-column :align="'center'" label="创建时间" prop="createTime" width="180">
                <template #default="scope">
                    <span>{{ $parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column :align="'center'" label="备注" prop="remark" />
            <el-table-column :align="'center'" class-name="small-padding fixed-width" width="220px" label="操作">
                <template #default="scope">
                    <el-button v-hasPerm="['system:sysBaseDictType:edit']" type="success"
                        @click="handleUpdate(scope.row)"><template #icon>
                            <i-ep-edit />
                        </template>修改
                    </el-button>
                    <el-button v-hasPerm="['system:sysBaseDictType:remove']" type="danger"
                        @click="handleDelete(scope.row)"><template #icon>
                            <i-ep-delete />
                        </template>删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNum"
            :total="total" @pagination="getList" />

        <!-- 添加或修改字典类型对话框 -->
        <el-dialog :close-on-click-modal="false" :title="title" v-model="open" append-to-body width="500px">
            <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
                <el-row :gutter="40">
                    <el-col :span="24">
                        <el-form-item label="字典名称" prop="baseDictName">
                            <el-input v-model="form.baseDictName" placeholder="请输入字典名称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="字典类型" prop="dictType">
                            <el-input v-model="form.dictType" placeholder="请输入字典类型" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="状态">
                            <el-radio-group v-model="form.status">
                                <el-radio v-for="dict in dictDataMap['sys_normal_disable']" :key="dict.dictValue"
                                    :value="dict.dictValue">{{ dict.dictLabel }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="form.remark" placeholder="请输入内容" type="textarea" />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script lang="ts" setup>
import {
    addSysBaseDictType,
    delSysBaseDictType,
    getSysBaseDictType,
    refreshCache,
    selectPageSysBaseDictType,
    updateSysBaseDictType,
} from "@/api/system/sysBaseDictType";
import { useDictStoreHook } from "@/store/modules/dict";

const dictStore = useDictStoreHook();
const formRef = ref()
const dictDataMap = ref(dictStore.isTypeGetData('sys_normal_disable'))
// 选中数组
const ids = ref([])
// 非单个禁用
const single = ref(true)
// 非多个禁用
const multiple = ref(true)
// 显示搜索条件
const showSearch = ref(true)
// 总条数
const total = ref(0)
// 字典类型表格数据
const sysBaseDictTypeList = ref([])
// 弹出层标题
const title = ref("")
// 是否显示弹出层
const open = ref(false)
// 日期范围
const dateRange = ref([])
// 查询参数
const queryParamsRef = ref()
const queryParams = ref({
    pageNum: 1,
    pageSize: 10,
    dictName: undefined,
    dictType: undefined,
    status: '',
    systemType: ''
})
// 表单参数
const form = ref<any>({})
const buttonLoading = ref(false)

const rules = ref({
    baseDictId: [
        { required: true, message: "字典主键不能为空", trigger: "blur" }
    ],
    baseDictName: [
        { required: true, message: "字典名称不能为空", trigger: "blur" }
    ],
    dictType: [
        { required: true, message: "字典类型不能为空", trigger: "change" }
    ],
    status: [
        { required: true, message: "状态不能为空", trigger: "blur" }
    ],
})
getList()
/** 查询字典类型列表 */
function getList() {
    selectPageSysBaseDictType(queryParams.value).then((response: any) => {
        sysBaseDictTypeList.value = response.rows;
        total.value = response.total;
    });
}
// 取消按钮
function cancel() {
    open.value = false;
    reset();
}
// 表单重置
function reset() {
    form.value = {
        baseDictId: undefined,
        baseDictName: undefined,
        dictType: undefined,
        status: "0",
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined
    };
}

function refreshCacheDictTypeFun() {
    refreshCache().then(() => {
        ElMessage.success("刷新成功");
    });
}
/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}
/** 重置按钮操作 */
function resetQuery() {
    queryParamsRef.value.resetFields()
    handleQuery();
}
/** 新增按钮操作 */
function handleAdd() {
    reset();
    open.value = true;
    title.value = "添加字典类型";
}
// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.baseDictId)
    single.value = selection.length != 1
    multiple.value = !selection.length
}
/** 修改按钮操作 */
function handleUpdate(row) {
    reset();
    const baseDictId = row.baseDictId || ids.value
    getSysBaseDictType(baseDictId).then((response: any) => {
        form.value = response.data;
        open.value = true;
        title.value = "修改字典类型";
    });
}
/** 提交按钮 */
function submitForm() {
    (formRef.value as any).validate(valid => {
        if (valid) {
            buttonLoading.value = true;
            if (form.value.baseDictId != null) {
                updateSysBaseDictType(form.value).then(_response => {
                    ElMessage.success("修改成功");
                    open.value = false;
                    getList();
                }).finally(() => {
                    buttonLoading.value = false;
                });
            } else {
                addSysBaseDictType(form.value).then(_response => {
                    ElMessage.success("新增成功");
                    open.value = false;
                    getList();
                }).finally(() => {
                    buttonLoading.value = false;
                });
            }
        }
    });
}
/** 删除按钮操作 */
function handleDelete(row) {
    const baseDictIds = row.baseDictId || ids.value;
    ElMessageBox.confirm('是否确认删除字典类型编号为"' + baseDictIds + '"的数据项？',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    ).then(() => {
        delSysBaseDictType(baseDictIds).then(() => {
            getList();
            ElMessage.success("删除成功");
        })
    }).catch(() => {
    })
}
/** 导出按钮操作 */
function handleExport() {
    //         this.download('system/sysBaseDictType/export', {
    //             ...this.queryParams
    //         }, `sysBaseDictType_${new Date().getTime()}.xlsx`)
}
</script>
<script lang="ts">
export default {
    name: "SysBaseDictType"
};
</script>
